package com.hn.kiss.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.kiss.config.aspect.LimitSubmit;
import com.hn.kiss.config.common.CommonPage;
import com.hn.kiss.config.common.CommonResult;
import com.hn.kiss.config.compoent.BaseController;
import com.hn.kiss.config.query.QueryGenerator;
import com.hn.kiss.config.util.UpLoadUtil;
import com.hn.kiss.modules.sys.entity.Tt;
import com.hn.kiss.modules.sys.service.ITtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;


@Api(tags = "TtController", description = "测试管理")
@RestController
@RequestMapping("/sys/tt")
@Validated
public class TtController extends BaseController {

    @Autowired
    ITtService ttService;

    @ApiOperation("测试分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<Tt> pageInfo(@NotNull Integer pageNo,
                                   @NotNull Integer pageSize,
                                   Tt tt,
                                   HttpServletRequest request){
        Page<Tt> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<Tt> qw = QueryGenerator.initQueryWrapper(tt, request.getParameterMap());
        Page<Tt> page1 = ttService.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("通过主键获取测试")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{id}")
    @LimitSubmit(key = "sys-tt-getByPk", limit = 10)
    public CommonResult<Tt> getByPk(@NotBlank@PathVariable("id") String id){
        Tt tt = ttService.getByPk(id);
        return CommonResult.success(tt);
    }

    @ApiOperation("新增测试")
    @PostMapping
    public CommonResult add(@Validated Tt tt, BindingResult bindingResult){
    boolean b = ttService.save(tt);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改测试")
    @PutMapping
    public CommonResult updateByPk(@Validated Tt tt, BindingResult bindingResult){
        boolean b = ttService.updateByPk(tt);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除测试")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public CommonResult removeByPk(@NotBlank@PathVariable("id") String id){
        boolean b = ttService.removeByPk(id);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("用户头像上传")
    @ApiImplicitParam(name = "name", value = "name", required = true, dataType = "String", paramType = "query")
    @PostMapping(value="/headImgUpload",consumes="multipart/*", headers="content-type=multipart/form-data")
    public CommonResult headImgUpload(@RequestParam("file") MultipartFile file, String name) throws IOException {
        String path = UpLoadUtil.uploadBeltDate(file, "user/headImg");
        System.out.println(name);
        return CommonResult.success(path);
    }



}
